package com.yl.auth.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.protocol.ResultsetRow;
import com.yl.auth.entity.Caigoushenqing;
import com.yl.auth.entity.Caigoushenqing_shicai;
import com.yl.auth.entity.Shicai;
import com.yl.auth.model.vo.CaigoushenqingVo;
import com.yl.auth.service.CaigoushenqingService;
import com.yl.auth.service.Caigoushenqing_shicaiService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-07-07
 */
@RestController
@RequestMapping("/admin/auth/caigoushenqing")
@Api(tags = "机构端-后勤管理")
public class CaigoushenqingController {
    @Resource
    private CaigoushenqingService caigoushenqingService;
    @Resource
    private Caigoushenqing_shicaiService caigoushenqing_shicaiService;

    /**
     * 采购申请
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getList/{page}/{limit}")
    @ApiOperation(value = "采购申请")
    public Result getList(@PathVariable @ApiParam("页码") Long page,
                          @PathVariable @ApiParam("记录") Long limit,
                          @ApiParam("机构名称") String ylName,
                          @ApiParam("到货时间") String daohuoTime,
                          @ApiParam("状态字段") Integer status){
        Page<CaigoushenqingVo> page1=new Page<>(page,limit);
        Page<CaigoushenqingVo> list=caigoushenqingService.getList(page1,ylName,daohuoTime,status);
        return Result.ok(list);
    }

    /**
     * 查询采购申请表的状态字段
     */
    @ApiOperation(value = "查询状态字段")
    @GetMapping("/getStatus/{id}")
    public Result getStatus(@PathVariable @ApiParam("采购id") Integer id){
        CaigoushenqingVo caigoushenqingVo=caigoushenqingService.getStatus(id);
        return Result.ok(caigoushenqingVo);
    }

    /**
     * 根据采购记录id查看采购详情-上半页面
     */
    @ApiOperation(value = "采购详情上")
    @GetMapping("/getCaiGouXiangQings/{id}")
    public Result getCaiGouXiangQings(@PathVariable @ApiParam("采购id") Long id){
        CaigoushenqingVo caigoushenqingVo=caigoushenqingService.getCaiGouXiangQings(id);
        return Result.ok(caigoushenqingVo);
    }

    /**
     * 根据采购记录id查看采购详情-下半页面
     */
    @ApiOperation(value = "采购详情下")
    @GetMapping("/getCaiGouXiangQingx/{id}")
    public Result getCaiGouXiangQingx(@PathVariable @ApiParam("采购id") Long id){
        List<CaigoushenqingVo> list =caigoushenqingService.getCaiGouXiangQingx(id);
        return Result.ok(list);
    }

    /**
     * 根据采购id删除采购记录-得同时删除采购食材中间表的记录-多表删除
     */
    @ApiOperation(value = "删除采购记录+中间表记录")
    @DeleteMapping("/remove/{id}")
    public void remove(@PathVariable @ApiParam("采购id") Integer id){
        caigoushenqingService.delete(id);
    }

    /**
     * 删除食材表数据
     */
    @ApiOperation(value = "删除食材表记录")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable @ApiParam("采购id") Integer id){
        caigoushenqingService.deleteById(id);
        return Result.ok();
    }

    /**
     * 采购申请编辑方法
     * 点击编辑按钮跳转到编辑页面，首先个根据采购表的id查询这条记录所采购的食材和采购的数量，
     * 第二部就是写一个编辑采购数量的方法
     */
//    @ApiOperation(value = "编辑方法")
//    @GetMapping("/")

    /**
     * 点击选择查询食材
     */
    @ApiOperation(value = "点击选择查询食材")
    @GetMapping("/getShiCai")
    public Result getShiCai(){
        List<CaigoushenqingVo> list=caigoushenqingService.getShiCai();
        return Result.ok(list);
    }

    /**
     * 撤回按钮业务——修改状态字段为待提交
     */
    @ApiOperation(value = "撤回按钮业务")
    @PutMapping("/updateStatus/{id}")
    public Result updateStatus(@PathVariable Integer id){
        caigoushenqingService.updateStatus(id);
        return Result.ok();
    }

    /**
     * 提交按钮业务--修改状态字段为待发货
     */
    @ApiOperation(value = "提交按钮业务")
    @PutMapping("/updateStatuss/{id}")
    public Result updateStatuss(@PathVariable Integer id){
        caigoushenqingService.updateStatuss(id);
        return Result.ok();
    }

    /**
     * 发货按钮业务--修改状态字段为待收货
     */
    @ApiOperation(value = "发货按钮业务")
    @PutMapping("/updateStatusss/{id}")
    public Result updateStatusss(@PathVariable Integer id){
        caigoushenqingService.updateStatusss(id);
        return Result.ok();
    }

    //回显员工全部数据
    //

//    @PostMapping("insert")
//    @ApiOperation(value = "23453245")
//    public Result insert(@RequestBody Caigoushenqing caigoushenqing){
//        //设默认值
//        caigoushenqing.setYgId(1);
//        caigoushenqing.setStatus(1);
//        caigoushenqingService.save(caigoushenqing);
//        return Result.ok();
//    }

    //添加中间表
    @GetMapping("/baocun")
    public Result baocun(@RequestParam("zhongjianbiaids") List<Integer> zhongjianbiaids){
        Caigoushenqing caigoushenqing1=new Caigoushenqing();
        caigoushenqing1.setYgId(1);
        caigoushenqing1.setStatus(1);
        caigoushenqingService.save(caigoushenqing1);
        List<Caigoushenqing> list = caigoushenqingService.list();
        Caigoushenqing caigoushenqing = list.get(list.size() - 1);
        int num=0;
        for (Integer zhongjianbiaid : zhongjianbiaids) {
            Caigoushenqing_shicai cg=new Caigoushenqing_shicai();
            cg.setSid(zhongjianbiaid);
            cg.setCid(caigoushenqing.getId());
            num++;
            cg.setNumber(num);
            caigoushenqing_shicaiService.save(cg);
        }
        return Result.ok();
    }
}

